/*
 * PartnerCombination.java
 *
 * Created on 10 de Julho de 2011, 15:08
 *
 */

package intopo.demonstrator.partnerclassification;

import util.adt.WeightedCollection;
import util.math.Rational;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 10 de Julho de 2011
 */
public class PartnerCombination
{
	/**
	 * The combination of partners.
	 */
	final WeightedCollection.Subset<Partner> combination;
	
	/**
	 * Creates a new instance of PartnerCombination
	 */
	public PartnerCombination (int combinationSize, WeightedCollection<Partner> partners)
	{
		this.combination = partners.subset (combinationSize, Cache.supply);
	}
	
	/**
	 * Perform the event combination of partners behaves acceptably and return {@code true} if it was sucessufull.
	 * 
	 * <p> We use the implementation of the Mersenne Twister pseudo-random number generator from <i>colt</i> library.  In particular we call the method {@code nextInt()} which returns 32-bit numbers.  We compare the result with a 32 bit number.
	 *
	 * <p> The user sees the probability of this event as a rational number.
	 */
	public boolean eventBehaveAcceptably ()
	{
		for (Partner p : this.combination) {
			if (!p.eventBehaveAcceptably ()) {
				return false;
			}
		}
		return true;
	}
	/**
	 * Return the probability that all partners in this combination behave acceptably.
	 */
	public Rational probabilityBehaveAcceptably ()
	{
		Rational result = Rational.ONE;
		for (Partner p : this.combination) {
			result = result.multiply (p.getBehaveAcceptablyProbability ());
		}
		return result;
	}
	/**
	 * Return the ids of the partners in this combination.
	 */
	public String ids ()
	{
		StringBuffer result = null;
		for (Partner p : this.combination) {
			if (result == null) {
				result = new StringBuffer (this.combination.size () * 5);
			}
			else {
				result.append (' ');
			}
			result.append (p.getId ());
		}
		return result.toString ();
	}
	/**
	 * Return a string with a representation of the partners in this combination.
	 */
	public String toString ()
	{
		StringBuffer result = null;
		for (Partner p : this.combination) {
			if (result == null) {
				result = new StringBuffer (this.combination.size () * 5);
			}
			else {
				result.append (' ');
			}
//			result.append (p.getBehaveAcceptablyProbability ().toString ());
			result.append (p.toString ());
		}
		return result.toString ();
	}
}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
